package com.ruoyi.service.impl;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.domain.T03;
import com.ruoyi.domain.T18;
import com.ruoyi.domain.T21;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.service.IT03Service;
import com.ruoyi.service.IT18Service;
import com.ruoyi.service.IT21Service;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.uitity.MinioUtil;
import net.sf.jsqlparser.expression.StringValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.T09Mapper;
import com.ruoyi.domain.T09;
import com.ruoyi.service.IT09Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 知识库管理Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-25
 */
@Service
public class T09ServiceImpl implements IT09Service
{
    @Autowired
    private T09Mapper t09Mapper;
    @Autowired
    private T18ServiceImpl t18ServiceImpl;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private MinioUtil minioUtil;
    @Autowired
    private IT09Service t09Service;
    @Autowired
    private IT03Service t03Service;
    @Autowired
    private IT21Service t21Service;


    /**
     * 查询知识库管理
     *
     * @param F0901 知识库管理主键
     * @return 知识库管理
     */
    @Override
    public T09 selectT09ByF0901(Long F0901)
    {
        return t09Mapper.selectT09ByF0901(F0901);
    }

    /**
     * 查询知识库管理列表
     *
     * @param t09 知识库管理
     * @return 知识库管理
     */
    @Autowired
    private TokenService tokenService;
    @Override
    public List<T09> selectT09List(T09 t09, HttpServletRequest request)
    {
        Long userId=tokenService.getLoginUser(request).getUser().getUserId();
        Long CompId = t18ServiceImpl.selectF0301ByF1801(userId);
        PageUtils.startPage();
        return t09Mapper.selectT09List(t09,CompId);
    }

    /**
     * 新增知识库管理
     *
     * @param t09 知识库管理
     * @return 结果
     */
    @Override
    public int insertT09(T09 t09)
    {
        return t09Mapper.insertT09(t09);
    }

    /**
     * 修改知识库管理
     *
     * @param t09 知识库管理
     * @return 结果
     */
    @Override
    public int updateT09(T09 t09)
    {
        return t09Mapper.updateT09(t09);
    }

    /**
     * 批量删除知识库管理
     *
     * @param F0901s 需要删除的知识库管理主键
     * @return 结果
     */
    @Override
    public int deleteT09ByF0901s(Long[] F0901s)
    {
        return t09Mapper.deleteT09ByF0901s(F0901s);
    }

    /**
     * 删除知识库管理信息
     *
     * @param F0901 知识库管理主键
     * @return 结果
     */
    @Override
    public int deleteT09ByF0901(Long F0901)
    {
        return t09Mapper.deleteT09ByF0901(F0901);
    }
    @Override
    @Transactional
    public boolean upload(Long userId,MultipartFile file, String filename) {
        //查找到这个用户的部门ID
//        Long deptId = sysUserService.selectUserById(userId).getDeptId();
//        //
//        SysDept sysDept = sysDeptService.selectDeptById(deptId);
//
//        Long parentId = sysDept.getParentId();
//        Long Id= t03Service.selectByF1801TOF0301(userId);
        Long  Id = t18ServiceImpl.selectF0301ByF1801(userId);

        T18 t18 = t18ServiceImpl.selectT18ByF1801(userId);
        Long comId = t18.getF0301();
        T03  t03 = t03Service.selectT03ByF0301(comId);
        T09 t09 = new  T09();
        t09.setF0301(Id);
        t09.setF0910(t03.getF0302());
        t09.setF1801(userId);
        t09.setF0902(filename);
        t09.setF0909(t18.getF1804());
        int dotIndex = filename.lastIndexOf(".");
        String filetype =filename.substring(dotIndex + 1);
        t09.setF0904(filetype);
        Date date = new Date();
        t09.setF0905(date);
        t09.setF0906(1L);
        t09.setF0908(filename);
        t09Service.insertT09(t09);
        minioUtil.uploadBigFile(file, filename);

        return true;
    }

    @Override
    public List<String> selectT09FilesByF0901s(Long[] f0901s) {
        return t09Mapper.selectT09FilesByF0901s(f0901s);
    }

    @Override
    @Transactional
    public AjaxResult approve(Long f0901) {
        T21 t21 = new T21();
        T09 t09 = t09Mapper.selectT09ByF0901(f0901);
        t09.setF0906(0L);
        t09Mapper.updateT09(t09);
        int maxNumber = t21Service.selectMaxNumber(t09.getF0301());
        int nextNumber = maxNumber + 1;
        T21  newT21 = t21Service.selectT21ByF2105((long)maxNumber);
        t21.setF0301(newT21.getF0301());
        t21.setF2102(newT21.getF2102());
        String names = newT21.getF2106();
        List<String> files = JSON.parseArray(names, String.class);
        t21.setF2103(newT21.getF2103());
        t21.setF2104(newT21.getF2104());
        files.add(t09.getF0902());
        t21.setF2106(JSON.toJSONString(files));
        t21.setDelFlag("0");

        //修改目前正在使用地 版本号
        T21 uset21 = t21Service.selectT21ByF0301AndDelFlag(t09.getF0301(),"0");

        //把当前正在使用版本设置为1
        uset21.setDelFlag("1");
        t21Service.updateT21(uset21);
//        t21Service.insertT21(t21);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult notApprove(Long f0901, String reason) {
        T09 t09 = t09Mapper.selectT09ByF0901(f0901);
        t09.setF0906(2L);
        t09.setF0911(reason);
        t09Mapper.updateT09(t09);
        return AjaxResult.success();
    }
}
